package com.lds.config.center.client.scheduled;

import com.lds.config.center.client.service.ClientService;
import com.lds.config.center.client.service.ClientServiceImpl;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;

public class ClientScheduled implements ApplicationRunner {
    private final ClientService clientService = ClientServiceImpl.getInstance();

    @Scheduled(fixedDelayString = "${config.center.client.fixed-delay}")
    public void getAllItem() {
        clientService.getAllItem();
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Thread thread = new Thread(() -> {
            while (true) {
                clientService.notifyUpdate();

                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    // ignore
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }
}
